<?php

namespace App\Http\Controllers\Api;


use Illuminate\Support\Facades\Auth;
use PhpOffice\PhpWord\TemplateProcessor;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;

use Validator;

use App\Model\Checkgroup;
use App\Model\Checknegative;
use App\Model\Checkaccord;
use App\Model\Checkcommand;
use App\Model\Company;
use App\Model\Comcheck;
use App\Model\Comcheckdetail;
use App\Model\Area;

use Illuminate\Http\Request;
use App\Http\Requests\CheckRequest;

class AlidataController extends Controller
{
	
    public function getLocation()
    {
        header("Access-Control-Allow-Origin: *");
        $data = Comcheck::all();
        $location = [];
        $i = 0;
        foreach ($data as $k => $v) {
            if($v->lat) {
                $location[$i]['lng'] = $v->lng;
                $location[$i]['lat'] = $v->lat;
                $location[$i]['type'] = 1;
                $location[$i]['value'] = 9;
                $i++;
            }
        }
        return response()->json($location);
    }

    public function getPost(Request $request)
    {
        $data = $request->all();
        Log::info($data);
        return response()->json(['data' => $data], 200);
    }

    public function getRisk()
    {
        header("Access-Control-Allow-Origin: *");
        $comcheckdetail = new Comcheckdetail();
        $data = $comcheckdetail->getAlidata();
        $risk = [
                ['x'=>'10条以上','y'=>0],
                ['x'=>'5-10条','y'=>0],
                ['x'=>'1-5条','y'=>0],
                ['x'=>'0条','y'=>0]
        ];
        foreach ($data as $k => $v) {
            if($v->count > 10) {
                $risk[0]['y'] += 1;
            } elseif($v->count > 5)
            {
                $risk[1]['y'] += 1;
            }elseif($v->count > 1)
            {
                $risk[2]['y'] += 1;
            }else
            {
                $risk[3]['y'] += 1;
            }
        }
        return response()->json($risk);
    }
    
    public function getCompany()
    {
        header("Access-Control-Allow-Origin: *");
        $company = new Company();
        $data = $company->getAlidata();
        $area = Area::where(['pid'=>'370200'])->get();
        $location = [];

        foreach ($area as $k => $v) {
            $location[$k]['x'] = $v->name;
            $location[$k]['y'] = 0;
        }
        foreach ($area as $k => $v) {
            foreach ($data as $kk => $vv) {
                if($v->id == $vv->district) {
                    $location[$k]['y'] += 1;
                }
            }
        }
        return response()->json($location);
    }

    public function getMonth()
    {
        header("Access-Control-Allow-Origin: *");
        $data = Comcheck::all();
        $month = [
            ['x'=>'2019/01/01 00:00:00','y'=>0,'s'=>1],
            ['x'=>'2019/02/01 00:00:00','y'=>0,'s'=>1],
            ['x'=>'2019/03/01 00:00:00','y'=>0,'s'=>1],
            ['x'=>'2019/04/01 00:00:00','y'=>0,'s'=>1],
            ['x'=>'2019/05/01 00:00:00','y'=>0,'s'=>1],
            ['x'=>'2019/06/01 00:00:00','y'=>0,'s'=>1],
            ['x'=>'2019/07/01 00:00:00','y'=>0,'s'=>1],
            ['x'=>'2019/08/01 00:00:00','y'=>0,'s'=>1],
            ['x'=>'2019/09/01 00:00:00','y'=>0,'s'=>1],
            ['x'=>'2019/10/01 00:00:00','y'=>0,'s'=>1],
            ['x'=>'2019/11/01 00:00:00','y'=>0,'s'=>1],
            ['x'=>'2019/12/01 00:00:00','y'=>0,'s'=>1]
        ];
        foreach ($data as $k => $v) {
            foreach ($month as $kk => $vv) {
                $firstday = date('Y-m-01', strtotime($vv['x'])); 
                $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
                if($v->created < $lastday && $v->created > $firstday) {
                    $month[$kk]['y'] += 1;
                }
            }
        }
        return response()->json($month);
    }

}
